package com.lgkj.ui.manager;

import android.net.Uri;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import com.cb.ratingbar.CBRatingBar;
import com.lgkj.R;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.CommonUtils;
import com.lgkj.utils.HttpUtils;
import com.lgkj.utils.KeybordS;
import com.lgkj.utils.SharedPreferencesUtils;
import com.lgkj.utils.TitleBuilder;
import com.lgkj.view.SelectPicPopupWindow;

import org.devio.takephoto.app.TakePhotoActivity;
import org.devio.takephoto.model.TResult;
import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/7/7 0007.
 */

public class StylistAddActivity extends TakePhotoActivity {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_job)
    EditText etJob;
    @BindView(R.id.itb_img)
    ImageButton itbImg;
    @BindView(R.id.vb_favour)
    CBRatingBar vbFavour;
    private SelectPicPopupWindow menuWindow;
    private String imageId = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_style_list_add;
    }

    @Override
    public void initView() {
        new TitleBuilder(this).setTitleText("发型屋添加").setLeftIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            KeybordS.hideKeyboard(StylistAddActivity.this);
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.btn_take_photo:
                    CommonUtils.takeFromCapture(getTakePhoto(), 258, 395);
                    break;
                case R.id.btn_pick_photo:
                    CommonUtils.takeFromDocuments(getTakePhoto(), 258, 395);
                    //调用系统图库的意图
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        String url = CommonConstant.getTakeImagePath();
        Log.i(TAG, "showImg: " + url);
        upload(new File(url));
    }

    private void upload(File file) {
        showDiaLog("图片正在上传");
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(StylistAddActivity.this));
        HttpUtils.uploadImage(map, CommonConstant.uploadImg, file, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                toastLong("上传成功", 0);
                imageId = jsonObject.optString("id");
                Uri uri = Uri.fromFile(new File(CommonConstant.getTakeImagePath()));
                itbImg.setImageURI(uri);
            }

            @Override
            public void errorHttp(String msg) {
                toastLong("上传失败", 0);
            }
        });
    }

    /**
     * http://mda.lianguangtech.com/api/barber/barberAdd?barbershop_id=287&sid=35d74b9faa1550c010484802469748b1&barber_img_id=156&access_token=669b4cbc9eaf42ddbd6652a0f9720e36&barber_favour=5&barber_position=XP%E5%8F%AA%E5%98%BB%E5%98%BB%E5%98%BB%E5%98%BB%E5%A7%93%E5%90%8D%E4%BD%A0%E7%88%B9%E5%A6%88&barber_name=%E6%B5%8B%E8%AF%95&barber_text=barber_text
     */
    private void save() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", CommonUtils.getToken(StylistAddActivity.this));
        map.put("barbershop_id", SharedPreferencesUtils.getParam(StylistAddActivity.this, "shop_id", "").toString());
        map.put("barber_name", etName.getText().toString());
        map.put("barber_favour", vbFavour.getStarCount() + "");
        map.put("barber_position", etJob.getText().toString());
        map.put("barber_text", "barber_text");
        map.put("barber_img_id", imageId);
        HttpUtils.postHttp(map, CommonConstant.hairstyleAdd, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                toastLong(jsonObject.optString("msg"), 0);
                if (jsonObject.optInt("code") == 0) {
                    finish();
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    @OnClick({R.id.itb_img, R.id.ibtn_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.itb_img:
                menuWindow = new SelectPicPopupWindow(StylistAddActivity.this, itemsOnClick);
////        menuWindow.sethideTake();
//                    //显示窗口
                menuWindow.showAtLocation(StylistAddActivity.this.findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                break;
            case R.id.ibtn_save:
                save();
                break;
        }
    }

    @Override
    public void initData() {

    }
}
